package cn.jizi.bshisy.controller;

import cn.jizi.bshisy.entity.PayItems;
import cn.jizi.bshisy.service.PayItemsService;
import cn.jizi.bshisy.tool.PageSupport;
import cn.jizi.bshisy.tool.ShuiJi;
import io.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping(value = "/payItems")
public class PayItemsController {
    private Logger logger = Logger.getLogger(PayItemsController.class);
    @Resource
    private PayItemsService payItemsService;

    /*添加收费信息*/
    @ApiOperation(value = "添加一条收费信息", notes = "添加一条收费信息")
    @PostMapping(value = "/getAddPayItems")
    public String getAddPayItems(PayItems payItems) {
        payItems.setPayDate(new Date());
        payItems.setPno(ShuiJi.random());
        int count = payItemsService.getAddPayItems(payItems);
        if (count > 0) {
            return "添加成功";
        } else {
            return "添加失败";
        }
    }

    // 查询收费数据，支持模糊查询，分页功能
    @ApiOperation(value = "根据收费项目类型，病人姓名模糊查询查询并分页", notes = "根据收费项目类型，病人姓名模糊查询查询并分页")
    @GetMapping(value = "/getPayItemsListPage")
    public List<PayItems> getPayItemsListPage(Model model, String pname, String sname, String pageIndex) {
        logger.info("==========================================》进入");
        logger.info("pname==================>" + pname);
        logger.info("sname==================>" + sname);
        logger.info("pageIndex==================>" + pageIndex);
        List<PayItems> payItemsList = null;
        // 设置页面容量
        int pageSize = 3;
        // 当前页码
        int currentPageNo = 1;
        // 设置页码数
        if (pageIndex != null) {
            currentPageNo = Integer.valueOf(pageIndex);
        }
        // 总数量（表）
        int totalCount = 0;
        try {
            totalCount = payItemsService.getPageListPayItemsCount(pname, sname);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 总页数
        PageSupport pages = new PageSupport();
        pages.setCurrentPageNo(currentPageNo);
        pages.setPageSize(pageSize);
        pages.setTotalCount(totalCount);
        int totalPageCount = pages.getTotalPageCount();
        // 控制首页和尾页
        if (currentPageNo < 1) {
            currentPageNo = 1;
        } else if (currentPageNo > totalPageCount) {
            currentPageNo = totalPageCount;
        }
        try {

            payItemsList = payItemsService.getPageListPayItems(pname, sname, currentPageNo, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        logger.info("payItemsList========================" + payItemsList);
        model.addAttribute("pages", pages);
        model.addAttribute("payItemsList", payItemsList);
        model.addAttribute("totalPageCount", totalPageCount);
        model.addAttribute("totalCount", totalCount);
        model.addAttribute("currentPageNo", currentPageNo);
        model.addAttribute("pname", pname);
        model.addAttribute("sname", sname);
        return payItemsList;
    }
}
